﻿#region Imports
using System;
using System.Diagnostics;
using System.Windows.Input;
using System.Windows.Threading;
using CommonUtilities.Util;
#endregion

// INTENTIONAL_SYNTAX_ERROR

namespace CommonUtilities.Commands
{
    /// <summary>
    /// http://wpfmvvmbindingext.codeplex.com/
    /// http://wpfmvvmbindingiocext.codeplex.com/
    /// http://msdn.microsoft.com/en-us/magazine/dn237302.aspx
    /// 
    /// Code from http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090030
    /// </summary>
    public class RelayCommand : ICommand
    {
        #region Declarations
        readonly Action<object> _execute = null;
        readonly Predicate<object> _canExecute = null;
        #endregion Declarations

        #region Properties
        public BaseViewModel viewModel { get; set; }
        #endregion Properties

        #region Constructors
        public RelayCommand(Action<object> execute, BaseViewModel viewModel = null)
            : this(execute, null, viewModel)
        {
        }

        public RelayCommand(Action<object> execute, Predicate<object> canExecute, BaseViewModel viewModel = null)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");

            _execute = execute;
            _canExecute = canExecute;
            this.viewModel = viewModel;
        }
        #endregion Constructors

        #region ICommand implementation
        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute(parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameter)
        {
            Dispatcher.CurrentDispatcher.BeginInvoke(
                new Action(() =>
                {
                    if (viewModel != null) viewModel.IsBusy = true;
                    _execute(parameter);
                    if (viewModel != null) viewModel.IsBusy = false;
                }));
        }
        #endregion
    }

    /// <summary>
    /// <code>CommonUtilities.Util.RelayCommand</code> with typed parameter <code>T</code>
    /// </summary>
    /// <typeparam name="T">Type of data to be passed as parameter into <code>CommonUtilities.Util.RelayCommand&lt;T&gt;</code></typeparam>
    public class RelayCommand<T> : ICommand
    {
        #region Declarations
        readonly Action<T> _execute = null;
        readonly Predicate<T> _canExecute = null;
        #endregion Declarations

        #region Properties
        public BaseViewModel viewModel { get; set; }
        #endregion Properties

        #region Constructors
        /// <summary>
        /// Creates a new command
        /// </summary>
        /// <param name="execute">The execution logic</param>
        /// <param name="viewModel">ViewModel for busy-indicator, only if defined in caller</param>
        public RelayCommand(Action<T> execute, BaseViewModel viewModel = null)
            : this(execute, null, viewModel)
        {
        }

        /// <summary>
        /// Creates a new command
        /// </summary>
        /// <param name="execute">The execution logic</param>
        /// <param name="canExecute">The execution status logic</param>
        /// <param name="viewModel">ViewModel for busy-indicator, only if defined in caller</param>
        public RelayCommand(Action<T> execute, Predicate<T> canExecute, BaseViewModel viewModel = null)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");

            _execute = execute;
            _canExecute = canExecute;
            this.viewModel = viewModel;
        }
        #endregion

        #region ICommand implementation
        [DebuggerStepThrough]
        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute((T)parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameter)
        {
            Dispatcher.CurrentDispatcher.BeginInvoke(
                new Action(() =>
                {
                    if (viewModel != null) viewModel.IsBusy = true;
                    _execute((T)parameter);
                    if (viewModel != null) viewModel.IsBusy = false;
                }));
        }
        #endregion
    }
}
